package com.example.myys.ui.base

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.myys.R
import com.example.myys.ui.theme.MyYsTheme

@Composable
fun SearchBar(
    modifier: Modifier = Modifier,
    onSearchClick: (String) -> Unit
) {
    var query by remember { mutableStateOf("") }
    Row(
        modifier = Modifier.padding(4.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        TextField(
            value = query,
            onValueChange = { query = it },
            placeholder = {
                Text(stringResource(id = R.string.placeholder_result))
            },
            modifier = modifier // 这里的modifier是直接在外部的操作下再做级联操作的更改
                .heightIn(min = 56.dp)
                .weight(1f),
            shape = MaterialTheme.shapes.medium,
        )
        Button(
            modifier = Modifier
                .width(100.dp)
                .heightIn(min = 56.dp)
                .padding(start = 3.dp),
            shape = MaterialTheme.shapes.medium,
            onClick = { onSearchClick(query) }) {
            Text(text = "搜索")
        }
    }

}

@Preview(showBackground = true)
@Composable
fun SearchBarPreview() {
    var query by remember { mutableStateOf("Hello") }
    MyYsTheme {
        SearchBar(
            onSearchClick = {}
        )
    }
}